package com.wwy.arithmetic.suanfa;

/**
 * 整数反转
 */
public class ZhengShuFanZhuan {

    public static void main(String[] args) {
        int fanzhuan = fanzhuan(-2147483648);
        System.out.println("fanzhuan = " + fanzhuan);
    }
    static int fanzhuan(int x){
        boolean flag = x < 0;
        String s = x+"";
        s = flag ?s.substring(1,s.length()):s;
        char[] chars = s.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (char aChar : chars) {
            sb.insert(0,aChar);
        }
        Long aLong = Long.valueOf(sb.toString());
        if(aLong>2147483648L){
            return 0;
        }else{
            int i = Integer.parseInt(sb.toString());
            return flag? i-2*i:i;
        }
    }
}
